package assets
{
	import away3d.textures.BitmapTexture;
	
	import events.LoadedEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	/**
	 * 未经压缩便上传给GPU的老式贴图资源（现已被AssetAtfTexture代替）
	 */	
	public class AssetBitmapTexture extends EventDispatcher
	{
		///////////////////////////////////////////////
		private var loadingList:Array = new Array;
		private var currentLoadingURL:String;
		private var lc:LoaderContext = new LoaderContext(true);
		private var loader:Loader;
		
		public function AssetBitmapTexture()
		{
			loader = new Loader;
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
		}
		
		private function onError(e:IOErrorEvent):void
		{
			var errorInfo:String ="没有此资源："+ currentLoadingURL;
			throw new Error(errorInfo);
		}
		
		public function load(difURL:String,opaURL:String=null):void
		{
			var url:String;
			if(opaURL)
			{
				url = difURL+","+opaURL;
			}else
			{
				url = difURL;
			}
			
			if(loadingList.indexOf(url) > -1)return;
			
			if(Asset.resBitmapTextures[url] == undefined)
			{
				loadingList.push(url);
				
				if(loadingList.length==1)loadNext();
				
			}else{
				dispatchEvent(new LoadedEvent(url,LoadedEvent.TEXTURE_COMPLETE));
			}
		}
		
		///////////////////////////////////////////////
		private function loadNext():void
		{
			var _difURL:String;
			
			currentLoadingURL = loadingList[0];
		
			var urls:Array = currentLoadingURL.split(",");
			
			var needMix:Boolean = false;
			if(urls.length==2 && urls[0]!=urls[1])needMix = true;
			
			_difURL = urls[0];
			
			if(needMix)
			{
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadOPacity);
			}else
			{
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
			}
			loader.load(new URLRequest(_difURL),lc);
		}
		
		private function onLoadComplete(e:Event):void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoadComplete);
			
			var bmd:BitmapData = ((loader.content) as Bitmap).bitmapData.clone();
			Asset.resBitmapTextures[currentLoadingURL] = new BitmapTexture(bmd);
			loadingList.splice(0,1);
			dispatchEvent(new LoadedEvent(currentLoadingURL,LoadedEvent.TEXTURE_COMPLETE));
			
			if(loadingList.length>0)currentLoadingURL = loadingList[0];
			
			if(loadingList.length == 0)
			{
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoadComplete);
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loadOPacity);
				return;
			}
			
			loadNext();
		}
		
		private function loadOPacity(e:Event):void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loadOPacity);
			
			var urls:Array = currentLoadingURL.split(",");
			var _difURL:String = urls[0];
			var _opaURL:String = urls[1];
			
			Asset.resBitmapDatas[_difURL] = ((loader.content) as Bitmap).bitmapData.clone();
			var bmd:BitmapData = Asset.resBitmapDatas[_difURL];
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,opaBitmaptextureComplete);
			loader.load(new URLRequest(_opaURL),lc);
		}
		
		private function opaBitmaptextureComplete(e:Event):void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,opaBitmaptextureComplete);
			var urls:Array = currentLoadingURL.split(",");
			var _difURL:String = urls[0];
			var rgbVal:BitmapData = Asset.resBitmapDatas[_difURL];
			
			var aValSour:BitmapData =((loader.content) as Bitmap).bitmapData.clone();
			var aVal:BitmapData;
			
			var wid:uint = rgbVal.width;
			var hei:uint = rgbVal.height;
			
			if(aValSour.width!=wid || aValSour.height!= hei)
			{
				aVal = new BitmapData(wid,hei);
				aVal.draw(aValSour,new Matrix(wid/aValSour.width,0,0,hei/aValSour.height));
			}else aVal = aValSour;
			
			var argbVal:BitmapData = new BitmapData(wid,hei,true,0xff000000);
			
			argbVal.copyChannel(rgbVal,rgbVal.rect,new Point,BitmapDataChannel.RED,BitmapDataChannel.RED);
			argbVal.copyChannel(rgbVal,rgbVal.rect,new Point,BitmapDataChannel.GREEN,BitmapDataChannel.GREEN);
			argbVal.copyChannel(rgbVal,rgbVal.rect,new Point,BitmapDataChannel.BLUE,BitmapDataChannel.BLUE);
			argbVal.copyChannel(aVal,aVal.rect,new Point,BitmapDataChannel.RED,BitmapDataChannel.ALPHA);
			
			Asset.disposeBitmapData(_difURL);
			
			Asset.resBitmapTextures[currentLoadingURL] = new BitmapTexture(argbVal);
			Asset.resBitmapDatas[currentLoadingURL] = argbVal.clone();
			
			loadingList.splice(0,1);
			dispatchEvent(new LoadedEvent(currentLoadingURL,LoadedEvent.TEXTURE_COMPLETE));
			
			//-----------------
			if(loadingList.length>0)currentLoadingURL = loadingList[0];
			
			if(loadingList.length == 0)
			{
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoadComplete);
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loadOPacity);
				return;
			}
			
			loadNext();
		}
		
		
	}
}